﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LuaFramework;

public class TestReadAB : MonoBehaviour {

    private static List<AssetBundle> readedAB = new List<AssetBundle>();

	// Use this for initialization
	void Start () {
        ReadAB();
	}

    public static void ReadAB()
    {
        readedAB.Add(AssetBundle.LoadFromFile("Assets/" + AppConst.AssetDir + "/testRelation/" + "testRelation"));

        // 读依赖
        AssetBundleManifest manifest = readedAB[0].LoadAsset("AssetBundleManifest") as AssetBundleManifest;
        var dependence = manifest.GetAllDependencies("test2");
        foreach (var name in dependence)
        {
            readedAB.Add(AssetBundle.LoadFromFile("Assets/" + AppConst.AssetDir + "/testRelation/" + name));
        }
        var ab = AssetBundle.LoadFromFile("Assets/" + AppConst.AssetDir + "/testRelation/" + "test2");
        readedAB.Add(ab);
        var obj = ab.LoadAsset("Cube1");
        var instance = MonoBehaviour.Instantiate(obj) as GameObject;
        UnloadAB(false);

        GameObject.DestroyImmediate(obj, true);

        //if (Application.isPlaying)
        //{
        //    instance.GetComponent<Renderer>().material.shader = Shader.Find("Standard");
        //}
        //else
        //{
        //    instance.GetComponent<Renderer>().sharedMaterial.shader = Shader.Find("Standard");
        //}

    }

    public static void UnloadAB(bool unloadAllAsset = true)
    {
        foreach (var ab in readedAB)
        {
            if (ab != null)
            {
                ab.Unload(unloadAllAsset);
            }
        }
        readedAB.Clear();
    }

	// Update is called once per frame
	void Update () {
	
	}
}
